home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / clang / sb02.zip / WAVE.C < prev   
C/C++ Source or Header  |  1994-06-23  |  3KB  |  97 lines

  1. #include <stdio.h>
  2. #include <io.h>
  3. #include <unistd.h>
  4. #include <std.h>
  5. #include "sb.h"
  6.  
  7. #define memeql(s1,s2,n)    (!memcmp(s1,s2,n))
  8.  
  9. char *get_wav_data(int *len, unsigned long *Rate, char *Name)
  10. {
  11.     int handle;
  12.     char buffer[25];
  13.     short shortbuf;
  14.     char *Data = NULL;
  15.     int i;
  16.  
  17.     if((handle = open(Name,O_RDONLY|O_BINARY)) == -1)
  18.     return(NULL);
  19.     /* is it a file in "RIFF WAVE fmt" format ? */
  20.     /*      there's 4 bytes of rLen in between, not used here */
  21.     read(handle,buffer,16);
  22.     if(!(memeql(buffer,"RIFF",4) && memeql(&(buffer[8]),"WAVEfmt ",8))) {
  23.     goto EndFunc;
  24.     }
  25.     read(handle,&i,4);                   /* start of data from here */
  26.     read(handle,&shortbuf,2);
  27.     if(shortbuf != 1) {                  /* ID for PCM-files        */
  28.     goto EndFunc;
  29.     }
  30.     read(handle,&shortbuf,2);
  31.     if(shortbuf != 1 && shortbuf != 2) { /* Neither mono nor stereo */
  32.     goto EndFunc;
  33.     }
  34.     read(handle,Rate,4);
  35.     lseek(handle,i-8,SEEK_CUR);          /* goto start of Data      */
  36.     read(handle,buffer,4);
  37.     if(!memeql(buffer,"data",4)) {       /* must be data block      */
  38.     goto EndFunc;
  39.     }
  40.     read(handle,len,4);                  /* actual data length      */
  41.     Data = (char *)malloc(*len);
  42.     if(Data)
  43.     read(handle,Data,*len);
  44. EndFunc:
  45.      close(handle);
  46.      return(Data);
  47. }
  48.  
  49.  
  50.  
  51. int put_wav_data(char *data, unsigned long data_bytes, unsigned long samples, char *Name)
  52. {
  53.     char *riff_string;
  54.     char *wave_string;
  55.     char *data_string;
  56.     char *fmt_string;
  57.     unsigned long data_plus_header_bytes;
  58.     unsigned long fmt_block_len;
  59.     unsigned short Format;
  60.     unsigned short channels;
  61.     unsigned long bytes_per_second;
  62.     unsigned short bytes_per_sample;
  63.     unsigned short bits_per_sample;
  64.     int handle;
  65.  
  66.     if((handle = open(Name,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY)) == -1)
  67.     return(0);
  68.  
  69.     fmt_block_len=16;                    /* header length                */
  70.     Format=1;                            /* must be 1 (PCM) for sounds   */
  71.     channels=1;                          /* only 1 (MONO) supported      */
  72.     bytes_per_sample=1;                  /* as only 8-bit mono supported */
  73.     bits_per_sample=8;                   /*      "                       */
  74.     bytes_per_second=samples;            /*      "                       */
  75.     data_plus_header_bytes=20+fmt_block_len+data_bytes;    /* total filelength */
  76.     riff_string="RIFF";
  77.     wave_string="WAVE";
  78.     fmt_string="fmt ";
  79.     data_string="data";
  80.     write(handle,riff_string,4);
  81.     write(handle,&data_plus_header_bytes,4);
  82.     write(handle,wave_string,4);
  83.     write(handle,fmt_string,4);
  84.     write(handle,&fmt_block_len,4);
  85.     write(handle,&Format,2);
  86.     write(handle,&channels,2);
  87.     write(handle,&samples,4);
  88.     write(handle,&bytes_per_second,4);
  89.     write(handle,&bytes_per_sample,2);
  90.     write(handle,&bytes_per_sample,2);
  91.     write(handle,data_string,4);
  92.     write(handle,&data_bytes,4);
  93.     write(handle,data,data_bytes);
  94.     close(handle);
  95.     return(1);
  96. }
  97.